Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Getting variables from file

by przemek88 (Initiate)
on Jan 31, 2011 at 02:28 UTC ( #885187=perlquestion: print w/ replies, xml ) Need Help??
przemek88 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, sorry for my silly question. I need to get data from file, as variables, and send them by using form to cgi program. Unfortunatelly, I can't read them outside loop. I tried to write a subroutine and get them by using return function, but it didn't work. Any ideas? Thanks.

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; my $file; print "<input type=\"file\" name=\"$file\" size=\"40\"> "; open (FILE, "$file"); while (<FILE>) { chomp; my ($name1, $name2, $address, $phone) = split("\t"); } close (FILE); print '<FORM name="form" ACTION="cgi.pl" METHOD="POST">'; print "<input type=\"hidden\" name=\"name1\" value=\"$name1\"> "; print "<input type=\"hidden\" name=\"name2\" value=\"$name2\"> "; print "<input type=\"hidden\" name=\"address\" value=\"$address\"> "; print "<input type=\"hidden\" name=\"phone\" value=\"$phone\"> "; print '<input style="width: 70px; height: 20px "type="submit" value="O +K" />'

Comment on Getting variables from file
Download Code
Re: Getting variables from file
by GrandFather (Cardinal) on Jan 31, 2011 at 02:48 UTC

    Are you expecting to read multiple lines from the input file or just one? If you intend to read multiple lines and thus (I guess) generate multiple forms put the print lines inside the loop (although there are much better ways to do this).

    If you expect to read a single line then replace the loop (and the few lines before it) with:

    my $file = '...'; open my $inFile, '<', $file or die "Can't open $file: $!"; my $line = <$inFile>; chomp $line; my ($name1, $name2, $address, $phone) = split("\t"); close($inFile);

    As an aside, notice the use of the three parameter version of open and the use of a lexical file handle. Note too the check on the result of the open.

    True laziness is hard work

      Grandfather,

      I must admit that I'm checking the results of system calls less and less these days, thanks to Paul Fenwick's wonderful autodie module.

      It is quite impressive, and removes the necessity of a whole bunch of error checking.

      I especially like the Klingon programming proverb... ;-)



      What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
Re: Getting variables from file
by wind (Priest) on Jan 31, 2011 at 06:47 UTC
    This is only a stylistic suggestion, but you should look into using qq instead of double quotes for your strings so that you don't have to escape all the quotes in your html properties.
    print q{<FORM name="form" ACTION="cgi.pl" METHOD="POST">}; print qq{<input type="hidden" name="name1" value="$name1"> }; print qq{<input type="hidden" name="name2" value="$name2"> }; print qq{<input type="hidden" name="address" value="$address"> }; print qq{<input type="hidden" name="phone" value="$phone"> }; print q{<input style="width: 70px; height: 20px "type="submit" value=" +OK" />};
    - Miller
Re: Getting variables from file
by kiz (Monk) on Jan 31, 2011 at 10:12 UTC

    If you are looking to get the contents of the file into a simple string (a 'scalar') variable, you have a number of option.

    Roll your own.

    You can read a file into an array in one go, which terminates on End-Of_file:

    my $file = 'foo.bar'; open(FH, $file); # as you already have my @temp = <FH>; # pull all lines into a list close FH; # because we are good people my ($name1, $name2, $address, $phone) = split("\t", @temp[0]);

    Option 2: File::Slurp

    For most of my code, I use File::Slurp these days:

    use File::Slurp; my $file = 'foo.bar'; my $temp = read_file($file); my ($name1, $name2, $address, $phone) = split("\t", $temp);

    (and you do realise that your split should really be using '/' for the regexp, not quotes? It's working because perl will accept any non-alphanumeric as a regexp delimiter is certain cases. Clever Perl :) )



    -- Ian Stuart
    A man depriving some poor village, somewhere, of a first-class idiot.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://885187]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-10-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (130 votes), past polls