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

Re^2: Appending to a file.

by xavierarmadillo (Initiate)
on May 07, 2012 at 16:50 UTC ( [id://969293]=note: print w/replies, xml ) Need Help??


in reply to Re: Appending to a file.
in thread Appending to a file.

Wow thank you. I am used to vb and c#, not PERL. It is so different to me. I changed it to
sub addSecondary() { my ($email, $secondary) = @_; use Tie::File; tie @array, 'Tie::File', 'registration.dat'; for (@array) { s/0;nodata/1;$secondary/gi if /$email/ } untie @array; }
And it works now. What I don't get is why s/0;nodata/1;$secondary/gi if /$email/ works. There is no semicolon at the end and it is not like any function I have seen. I am used to something = something type notation. How does it even know what to look at and compare?

Replies are listed 'Best First'.
Re^3: Appending to a file.
by MidLifeXis (Monsignor) on May 07, 2012 at 17:24 UTC
    for (@array) { s/0;nodata/1;$secondary/gi if /$email/ }

    This would be (essentially) shorthand for:

    for my $_ (@array) # 'my $_' is a little bit of a fudge { $_ =~ s/0;nodata/1;$secondary/gi if $_ =~ /$email/ }

    See perlvar for a little more explanation.

    --MidLifeXis

Re^3: Appending to a file.
by tobyink (Canon) on May 07, 2012 at 18:58 UTC

    There's a global variable (though in recent versions of Perl it can be made more local on request) in Perl called $_. Many Perl functions, operators and looping constructs operate on $_ if you don't give them another variable to operate on instead.

    For example, rather than:

    foreach my $item (@array) { print($item); }

    You can take advantage of the fact that the default name used by foreach is $_, and the default thing printed by print is $_, and instead write:

    for (@array) # note "for" and "foreach" are synonyms { print; }

    Or just:

    print for @array;

    Applying the same logic to your loop, the for is assigning each item in @array to $_; and if /$email/ is shorthand for if $_ =~ /email/; etc.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Appending to a file.
by AnomalousMonk (Archbishop) on May 07, 2012 at 19:34 UTC

    xavierarmadillo: I see that in your latest posting the substitution
        s/nodata/$secondary/gi
    has become
        s/0;nodata/1;$secondary/gi
    adding '0;' and '1;' to the mix. Maybe this is intentional, but I just thought I'd mention it.

    Another point is that you are now defining the  addSecondary function with an empty prototype, meaning that the function takes no arguments — and then you pass it two arguments! This suggests to me that you are calling the function in a way that causes Perl (not PERL) to ignore prototypes. If prototypes do nothing in your program, or you don't fully understand them, or both, why bother to use them?

    The final and perhaps most important point is that it is wise always to enable warnings and strictures at the beginning of a program, and as you are a newcomer to Perl, to enable diagnostics. So your program would begin with the following lines:
        use warnings;
        use strict;
        use diagnostics;  # for good measure
    See warnings, strict and diagnostics.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://969293]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found