Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 886
ParoX Avatar asked Jun 29 '09 00:06

ParoX


People also ask

How do I write to a text file in Perl?

In order to write to the file, it is opened in write mode as shown below: open (FH, '>', “filename. txt”); If the file is existing then it truncates the old content of file with the new content.

How do you create and write to a file in Perl?

Syntax To Open File in write mode To write a file in Perl it is important to open a file. Filename state that open specified file for write mode to write any content into the file. Print() function is very important in Perl to write content into the file. We have written content into the file using a print function.

How do I write multiple lines in a file in Perl?

Multiline String using Single & Double QuotesUser can create a multiline string using the single(”) quotes and as well as with double quotes(“”).

How do I append to a file in Perl?

Step 1: Opening a file in read mode to see the existing content of the file. Step 2: Printing the existing content of the file. Step 3: Opening the File in Append mode to add content to the file. Step 6: Reading the file again to see the updated content.


2 Answers

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]: $!";
like image 143
Telemachus Avatar answered Oct 28 '22 23:10

Telemachus


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");
like image 41
Chas. Owens Avatar answered Oct 28 '22 22:10

Chas. Owens