ParoX
ParoX

Reputation: 5933

What's the easiest way to write to a file using Perl?

Currently I am using

system("echo $panel_login $panel_password $root_name $root_pass $port $panel_type >> /home/shared/ftp");

What is the easiest way to do the same thing using Perl? IE: a one-liner.

Upvotes: 7

Views: 8751

Answers (9)

Buridan
Buridan

Reputation: 1

You can do a one-liner like this one:

print "$panel_login $panel_password $root_name $root_pass $port $panel_type" >> io('/home/shared/ftp');

You only need to add the IO::All module to your code, like this:

use IO::All;

Upvotes: 0

Tom
Tom

Reputation: 45104

EDIT (By popular and editable demand)

http://perldoc.perl.org/functions/open.html

In your case you would have to :

  #21st century perl.
  my $handle;
  open ($handle,'>>','/home/shared/ftp') or die("Cant open /home/shared/ftp");
  print $handle "$panel_login $panel_password $root_name $root_pass $port $panel_type";
  close ($handle) or die ("Unable to close /home/shared/ftp");

Alternatively, you could use the autodie pragma (as @Chas Owens suggested in comments). This way, no check (the or die(...)) part needs to be used.

Hope to get it right this time. If so, will erase this Warning.

Old deprecated way

Use print (not one liner though). Just open your file before and get a handle.

open (MYFILE,'>>/home/shared/ftp');
print MYFILE "$panel_login $panel_password $root_name $root_pass $port $panel_type";
close (MYFILE);

http://perl.about.com/od/perltutorials/a/readwritefiles_2.htm

Upvotes: 6

Yonatan Broza
Yonatan Broza

Reputation: 361

You might want to use the simple File::Slurp module:

use File::Slurp;

append_file("/home/shared/ftp",
    "$panel_login $panel_password $root_name $root_pass ".
    "$port $panel_type\n");

It's not a core module though, so you'll have to install it.

Upvotes: 6

gpojd
gpojd

Reputation: 23065

I use FileHandle. From the POD:

use FileHandle;
$fh = new FileHandle ">> FOO"; # modified slightly from the POD, to append
if (defined $fh) {
    print $fh "bar\n";
    $fh->close;
}

If you want something closer to a "one-liner," you can do this:

use FileHandle;
my $fh = FileHandle->new( '>> FOO' ) || die $!;
$fh->print( "bar\n" );
## $fh closes when it goes out of scope

Upvotes: 0

user118435
user118435

Reputation: 173

Some good reading about editing files with perl:

FMTYEWTK About Mass Edits In Perl

Upvotes: -1

dwarring
dwarring

Reputation: 4883

For advanced one-liners like this, you could also use the psh command from Psh, a simple pure Perl shell.

 psh -c '{my $var = "something"; print $var} >/tmp/out.txt'

Upvotes: 0

Matthew Scharley
Matthew Scharley

Reputation: 132254

(open my $FH, ">", "${filename}" and print $FH "Hello World" and close $FH) 
    or die ("Couldn't output to file: ${filename}: $!\n");

Of course, it's impossible to do proper error checking in a one liner... That should be written slightly differently:

open my $FH, ">", "${filename}" or die("Can't open file: ${filename}: $!\n");
print $FH "Hello World";
close $FH;

Upvotes: 6

Chas. Owens
Chas. Owens

Reputation: 64919

You might find IO::All to be helpful:

use IO::All;
#stuff happens to set the variables
io("/home/shared/ftp")->write("$panel_login $panel_password $root_name $root_pass $port $panel_type");

Upvotes: 10

Telemachus
Telemachus

Reputation: 19705

Why does it need to be one line? You're not paying by the line, are you? This is probably too verbose, but it took a total of two minutes to type it out.

#!/usr/bin/env perl
use strict;
use warnings;

my @values = qw/user secret-password ftp-address/;

open my $fh, '>>', 'ftp-stuff'          # Three argument form of open; lexical filehandle
  or die "Can't open [ftp-stuff]: $!";  # Always check that the open call worked

print $fh "@values\n";     # Quote the array and you get spaces between items for free

close $fh or die "Can't close [ftp-stuff]: $!";

Upvotes: 13

Related Questions