Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Is 'use vars' really obsolete?

by LanX (Saint)
on Sep 22, 2017 at 01:38 UTC ( [id://1199860]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Is 'use vars' really obsolete?
in thread Is 'use vars' really obsolete?

That's lots of voodoo. ..

the effect of our will be restricted to the eval, I'm not sure how far use vars go.

I'd say it's globally effective whenever the same package is chosen.

(but I'm not really trying to understand what is happening during an export here. :)

edit

OK I think I have an idea what is happening here and I think the author should have a look into Exporter, IIRC exporting package-vars is part of the specification.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^4: Is 'use vars' really obsolete? (use vars mechanics)
by LanX (Saint) on Sep 22, 2017 at 12:00 UTC
    > I'd say it's globally effective whenever the same package is chosen.

    Basically yes.

    I had a look into the code of vars and found this discussion Confused by symbol table import mechanism of 'use vars'

    What's happening is a "fake" import:

    *Target::x = \$Target::x;

    Importing means that in the symbol table of the target package a glob is assigned to an external reference.

    But in this case it's just a self reference to its own package. (Granted, this looks a bit confusing... *)

    Like this strict doesn't complain when seeing an unqualified $var inside package Target because it's already imported into Target's STASH.

    To answer the OP, this has global effect while our is tightly scoped.

    And the use case you've shown is just a quite complicated way to export.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    *) the symbol table decides which variables, functions, file handles and formats are known to the package without needing to be fully qualified.

Re^4: Is 'use vars' really obsolete?
by perl-diddler (Chaplain) on Sep 22, 2017 at 20:16 UTC
    Did you catch the last line of my post:
    FWIW, the module being used, exported variable names with values corresponding to found paths of their lower-case names as executables, so any EXPORT list would be variable per usage (though fixed per program invocation).

    I didn't think Exporter would work in this case because, 1) I'm not exporting anything, and 2) the programs using the module get differently named IMPORTS based on what they pass (not based on anything in the used module -- i.e. I'm not exporting any package variables.

    Example: I had a program that created file-system snapshots. In main I needed to call ~10 external programs, so I had:

    use Cmds qw(sudo dd rsync dmsetup lvremove touch cp rm mount umount +);
    In places in the program where I wanted to call a program, I'd construct a command. Since the program ran part-time as root, I wanted to make sure that common commands resolved to the real commands in the right places. Additionally, I didn't want it to fail unpredictably -- but instead fail at startup there would be no changes to the system state. In the above usage, Cmds uses a safe internal path and then puts variables containing the absolute paths of the commands into the caller's package name space. The variables never exist in Cmds, so I don't see that Exporter would even work in this case.

    The calling program would reference the external command paths by using the up-cased name of the command as a variable. Example:

    open ($fh, "|$Sudo $Dd conv=fsync of=$fn 2>/dev/null");
    So Cmds allows me to call external commands, safely, using builtin paths, allows me to use those paths via variables that can be used in strings, and ensures presence of needed external commands before running the program.

    As for the use case being complicated, it was easier to make work 7 years ago than other methods. I just ran across it when checking something out in the module and it seemed like an interesting case where 'our' wasn't an easy replacement for use vars.

        Did you catch the last line of my post:
        No I have problems to parse the phrase structure, sorry.
        No, I'm sorry! Thank you for your patience. Anyway, ignoring that -- I tried to "re-explain it" -- why using Exporter would be a problem. I probably could do something similar with the "export_to_level" ... but why? It seems like overkill to pull in Exporter to do what took 3-4 lines of code in my module -- just to get "away" from using "use vars".

        On calling vars->import(List), do you mean in the program that's using the module? Where would var->import import things from? I.e. do you mean I'd do something like:

        use Cmds qw(sudo dd); vars->import(Sudo Dd);
        It seems cleaner to only specify the external cmd list once, but I'm not sure this is what you meant.

        As for setting the stash -- probably could but that'd involve getting more into the internals of perl and I'd prefer to not do that, since the perl internals could easily change in a new release and then I'd have to fix my usage of it. More work! ARG! :-)

        Why would I want to steer away from the eval? This is code that is usually only executed once at BEGIN time.

        While I may not want to go back and change this module, I probably do want to go change some other modules I care about more, I'm still not clear why I'd prefer to get deeper into perl internals to get away from a "generic" eval? Most of my modules work back to 5.8 and some back to 5.6. I usually try to spend sometime to use less advanced methods so I can be compatible with older perls. Not always successful, but I still try.

        -linda

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found