Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Pointers and References

by bliako (Prior)
on Nov 24, 2020 at 01:21 UTC ( #11124103=note: print w/replies, xml ) Need Help??


in reply to Re^3: Pointers and References
in thread Pointers and References

I have to say I come from C and back then (yes! Then) without goofle and the free literature it was a lot of hard work to conquer the pointers. I can't let it go now. I have often debated internally whether thinking in C while writing Perl was a good habit/idea/practice. It is NOT. But when %$^$& comes to shovel I have to think of some ways to make it faster. Because I am too lazy to program it in C eventually. And these are some of the tricks I pull.

That's why my subs most of the times have a hashref of params as input (as opposed to array-style (file => 'aaa' , action => 'delete')) and I always return a hashref or arrayref or object back, but never ever an array. I think somewhere an angel dies when I do that. Cargo-culting perhaps, plus I am not a natural Perl-er.

bw, bliako

Replies are listed 'Best First'.
Re^5: Pointers and References
by LanX (Cardinal) on Nov 24, 2020 at 12:17 UTC
    > ... as opposed to array-style ... but never ever an array ...

    Probably nitpicking: You say "array" but mean "list". :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Thanks for pointing this out LanX

      Perhaps that's the reason I return a (array)ref: I don't want to say list! Because when I think I grasped the distinction list vs array, something proves me wrong again. But hey when in Perl speak Perl (even if some/quite-a-few publications use the terms interchangebly). I would prefer anonymous, readonly (fixed-size) array of readonly (lest they be references) items.

      But here is a quick question: you can't modify a list item in say a foreach loop foreach my $x (1,2,3){ $x = 42 } (Modification of a read-only value attempted) but in the parameter list to a sub you can: $_[0] = 12 (as jcb demonstrated earlier).

      bw, bliako

        Lists in Perl are two things:

        • a syntax, mostly comma separated (, is a list operator)
        • a temporary stack to pass this sequence of scalars

        Array is

        • a @variable holding
        • a data structure in memory

        So @a = (1,2,3) means step by step

        • array variable @a at LHS forces a list assignment
        • the comma syntax denotes a list
        • the list values are pushed to a stack
        • the stack is assigned to the array and stored in memory in an AV

        I hope it's clearer now.

        If it's a consolation, Larry occasionally confused the terminology too, e.g. in wantarray :)

        > (Modification of a read-only value attempted)

        That's because 1,2,3 are literals which are read only. Use variables (sic) and you can change them in the loop. ;)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        ... you can't modify a list item in say a foreach loop ... but in the parameter list to a sub you can: $_[0] = 12 ...

        Further to LanX's reply:   And, of course, one cannot modify a literal value in an argument list even though the list is passed as an array (of aliases). In no case can one modify a literal value: 1 is, let us fervently hope, always 1.

        Win8 Strawberry 5.8.9.5 (32) Tue 11/24/2020 14:56:16 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -l sub f { return $_[0] = 42; } my $x = 88; print f($x); print $x; print f(99); ^Z 42 42 Modification of a read-only value attempted at - line 1.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-06-13 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?