Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Print HTML form to file using CGI.pm save method

by SerZKO (Beadle)
on Aug 03, 2012 at 19:25 UTC ( #985330=perlquestion: print w/ replies, xml ) Need Help??
SerZKO has asked for the wisdom of the Perl Monks concerning the following question:

Hej Monks,

I have a following code to save my form data to a file :

sub sparaData { open (KOF, ">$konfil") or die "Kan inte \xF6ppna filen $konfil"; flock (KOF, 2) or die "Kan inte l\xE5sa filen $konfil"; $qry-> save (*KOF) or die "Kan inte spara filen $konfil"; close KOF; }

Now, all of my variables that I get from a form are defined withing a subroutine (2nd map) that writes a form which is

sub kundNytt { my @Head = ("Anv. ID", "F\xF6r", "Efter", "Pref", "Bef", "Nytt", "Pr", + "F\xF6r", "Kap"); my $trans = $clrNr; open (TEMP, "<$tempfil") or die "Kan inte \xF6ppna filen $tempfil"; print $qry->start_form(-action => "http://skinnmaskin/cgi-bin/kon.cgi" +); print $qry->table( { Border => 1, Cellpadding => 5, bordercolor =>"#FFFFFF" }, Tr(map { th($_) } @Head), (map { my ($Uid, $Fn, $En, $Pr, $Ank, $Ext, $Or, $Adr) = split /\|/; Tr( td(textfield('UID', $Uid, 6)), td(textfield('FNA', $Fn, 20)), td(textfield('ENA', $En, 35)), td(textfield('PRE', $Pr, 10)), td(textfield('BEF', $Ext, 16)), td(textfield('NYTT', $Ank, 5)), hidden('ADRE', $Addr), td(checkbox_group('GRU',['P', 'F', 'K', 'Ej'],'Ej')), td(checkbox_group('DOL',['Ja', 'Nej'],'Nej')), td(checkbox_group('CMG',['Ja','Nej'],'Nej')), ); } <TEMP>), ); close TEMP; print $qry->hidden('kontor', $trans); print $qry->submit('spara', 'Addera rad'); print $qry->submit('spara', 'Spara'); print $qry->end_form; }

Is it possible make save method executes only if some of variables (e.g. $Ext) are defined (not empty) without exporting variables in "outer space" ?

Thanks in advance.

Comment on Print HTML form to file using CGI.pm save method
Select or Download Code
Re: Print HTML form to file using CGI.pm save method
by Anonymous Monk on Aug 03, 2012 at 19:36 UTC

    Is it possible make save method executes only if some of variables (e.g. $Ext) without exporting variables in "outer space" ?

    Yes. What does that mean?

      Aren't variables defined within subroutine valid only for that subroutine?

        Aren't variables defined within subroutine valid only for that subroutine?

        Sure (lexicals are), unless you return them

        One thing some beginning programmers (usually of the procedural discipline) often miss is that functions are supposed to be like their mathematical definition, f(x) = expr. That is, they generally take a value or three as input, and return a value as output. They aren't supposed to access variables outside their scope ("globals", as we call them in the programming world). Of course, there are half a dozen of exceptions to that rule.

        I can see that you are accessing the variables $clrNr, $tempfil, and $qry. I'll trust that the latter two are OK as globals, but the first one probably isn't. Here's how we pass it to your function:

        sub kundNytt { my ($trans) = @_; # do something with $trans }

        And here is how you call it.

        $clrNr = 42; kundNytt($clrNr);

        Your code, as it is currently, is fragile, and relies on $clrNr being correctly set before the function is called. It will break sooner or later. Seeing that functions are akin to their mathematical definition helps produce code that is much easier to understand and more reliable.

        Ah, this is probably covered in pretty much every programming book written in the last three decades. You could read one :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-09-16 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (13 votes), past polls