Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to write a subroutine content into a file

by Pal-Cod3r (Initiate)
on Nov 08, 2011 at 09:14 UTC ( [id://936691]=perlquestion: print w/replies, xml ) Need Help??

Pal-Cod3r has asked for the wisdom of the Perl Monks concerning the following question:

I tried to make a script that write a subroutine content into a file but when i run it i received in the file:
1
and the code could be like this:

sub doit{
print "How are you?\n";
}
open (FILE, ">files.txt");
print FILE &doit;
close(FILE);


what is the wrong in this code and what shall i do to receive a word (How are you?) in the files.txt
Please answer my question .
Best Wishes.
  • Comment on how to write a subroutine content into a file

Replies are listed 'Best First'.
Re: how to write a subroutine content into a file
by moritz (Cardinal) on Nov 08, 2011 at 09:21 UTC

    You call a subroutine that prints something to the default output handle, and returns 1 (because the print was successful). Then you write that 1 into FILE, which is opened to 'files.txt'.

    If you want the string "How are you?\n" to be end up in the file, you need to write

    print FILE "How are you?\n";

    Or something along the lines of

    open my $file, '>', 'files.txt'; select $file; # print to $file by default doit(); close $file; select STDOUT; # print to STDOUT again by default
Re: how to write a subroutine content into a file
by mrstlee (Beadle) on Nov 08, 2011 at 09:43 UTC
    sub doit{ return "How are you?\n"; } open (FILE, ">files.txt"); print FILE &doit; close(FILE);
    Or
    sub doit{ my $handle = shift; print $handle "How are you?\n"; } open (FILE, ">files.txt"); doit(*FILE); close(FILE);
    Or .. lots of ways.
Re: how to write a subroutine content into a file
by MVS (Monk) on Nov 08, 2011 at 09:44 UTC

    A simple way to correct your sample code would be to replace:

    print "How are you?\n";

    with:

    return "How are you?\n";

    Now, your subroutine is returning a string, so your print statement below will print the string itself rather than the results of your successful print statement.

    Edit: Oops, I didn't see the reply above until after I posted this.
Re: how to write a subroutine content into a file
by ansh batra (Friar) on Nov 08, 2011 at 10:23 UTC
    it might be funny but i thought Pal-Cod3r have diff requirement.

    sub doit{ print "How are you?\n"; $x=1+1; print "$x\n"; } open(FILE,"<monkss.pl"); @lines=<FILE>; close(FILE); open (FILE, ">files.txt"); $flag=0; foreach $line(@lines) { if($line =~ /doit\{/) { $flag=1; } if($flag) { print FILE $line; } if($line=~ /\}/) { $flag=0; } } close(FILE);

    contents of file.txt
    sub doit{ print "How are you?\n"; $x=1+1; print "$x\n"; }
      Or, similarly:
      #!/usr/bin/perl use warnings; use strict; sub doit { print "How are you?\n"; } seek DATA, 0, 0; while (<DATA>) { print if /^sub doit {/ .. /^}/ } __DATA__
Re: how to write a subroutine content into a file
by aquinom (Sexton) on Nov 08, 2011 at 14:22 UTC
    The question has already answered but I thought I would contribute something nonetheless: don't use &subroutine notation anymore, it's antiquated perl and can cause unexpected problems. simply "print doit(*FILE)" as in one of the answers would suffice.
      If &subroutine is antiquated Perl, what's the preferred way to reference a sub?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://936691]
Approved by Corion
Front-paged by ChuckularOne
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found