Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: How to import "global" variables into sub-scripts from main script?

by choroba (Cardinal)
on Mar 23, 2021 at 21:36 UTC ( [id://11130242]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to import "global" variables into sub-scripts from main script?
in thread How to import "global" variables into sub-scripts from main script?

> removing it should have no effect at runtime

I'm not sure about it. Symbolic reference can happen in a nested structure, see Can you use string as a HASH ref while "strict refs" in use?.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
  • Comment on Re^4: How to import "global" variables into sub-scripts from main script?
  • Download Code

Replies are listed 'Best First'.
Re^5: How to import "global" variables into sub-scripts from main script?
by haukex (Archbishop) on Mar 24, 2021 at 08:45 UTC

    This is Perl, of course it's possible to construct a counterexample ;-) Imagine someone wrote their own version of Data::Diver:

    use warnings; use strict; sub dive { my ($data, @path) = @_; die "unsafe keys" if grep { /[^a-zA-Z0-9_]/ } @path; return eval '$data->'.join('', map { "{$_}" } @path); } use Test::More tests=>6; sub exception (&) { eval { shift->(); 1 } ? undef : ($@ || die) } our %quz = ( quz => 'Hello!' ); our %foo = ( bar => 'quz', baz => { hello => "World!" } ); is dive(\%quz, qw/ quz /), 'Hello!'; is dive(\%foo, qw/ bar /), 'quz'; is dive(\%foo, qw/ bar x /), undef; is dive(\%foo, qw/ baz hello /), 'World!'; is dive(\%foo, qw/ bar quz /), undef; like exception { dive(\%foo, qw/ $hello /) }, qr/\bunsafe keys\b/i;

    Under no strict 'refs', the code will behave differently and the tests will fail.

    Any code with eval is susceptible - for a slightly more realistic scenario, imagine a templating system that is loading data from JSON, for example. Update: Though I think the code above isn't even that unrealistic, given the huge number of modules on CPAN there's bound to be one or two that do something like that. Also added a test case to the above code.

Re^5: How to import "global" variables into sub-scripts from main script?
by jcb (Parson) on Mar 24, 2021 at 01:32 UTC

    Assuming that all code paths are actually exercised in development/testing with strict in effect, I would expect a testing cycle to prove that the program is type-safe with respect to hard-references/other-data, such that symbolic references could not be encountered at runtime that were not seen in testing. This requires that the tests are actually thorough and near-exhaustive.

    For our questioner, that is a very large assumption and I would not be surprised to find that the code that produced this question has no testsuite.

    In terms of runtime performance, I doubt that strict 'refs' has any effect at all: perl must check for symbolic references in any case as it can never assume that a dereferenced value will actually be a reference and the only difference is whether a possible symbolic reference is considered or an error immediately thrown when dereferencing something other than a hard reference.

Re^5: How to import "global" variables into sub-scripts from main script?
by LanX (Saint) on Mar 24, 2021 at 02:54 UTC
    That's an interesting idea.

    This example dies under strict with Can't use string as a HASH ref and would "survive" with faulty magic after strict was removed.

    For this not to be noticed under strict the symbolic ref has only to happen occasionally.

    So yes it has an effect on runtime, but this is "only" exchanging one (fatal) bug with a weird bug in edge cases.

    So what could happen is that someone thought removing strict fixed the bug in a long running script, which eventually produced broken data.

    Well ... I think we agree that removing strict is not a good idea anyway.

    One might only be able to make a case for short scripts and one-liners. But the longer you wait to activate strict the more expensive it gets.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found