in reply to Re: Dereferencing in blessed object
in thread Dereferencing in blessed object

Thanks haukex - that's extremely helpful.

or, in the worst case, you're not using strict.

The main script has use strict; but it seems that does then apply to the module it calls...I know that now!

So now I've added use strict; I get a whole bunch of Global symbol requires explicit package name errors even though they all have my or our declarations. I tracked this down to the declaration happening later in the module than methods that are throwing the error.

It also highlighted another error with this:

$db =~ s/_.+?_/_$dbname_/;
The variable $dbname is declared but in the regexp it is looking for $dbname_. I have solved this for now with:
my $temp = "_$dbname"."_"; $db =~ s/_.+?_/$temp/;
But - I feel sure there is a more elegant way!

Replies are listed 'Best First'.
Re^3: Dereferencing in blessed object
by hippo (Archbishop) on Feb 27, 2021 at 22:44 UTC

    Use braces:

    $db =~ s/_.+?_/_${dbname}_/;

    🦛

      Or, use a backslash, because TIMTOWTDI:
      $db =~ s/_.+?_/_$dbname\_/;
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Use braces:

      That was my first thought - but I tried it and it didn't work...
      So...plenty of use of Ctrl+Z and I realised what I did wrong!

      $db =~ s/_.+?_/_{$dbname}_/;

      Not quite the same!
      Thanks hippo

        but I tried it and it didn't work

        Sure it does:

        use strict; use warnings; use Test::More tests => 1; my $db = 'a_foo_b'; my $dbname = 'bar'; $db =~ s/_.+?_/_${dbname}_/; is $db, 'a_bar_b';

        Update: I see from your reply that you had already figured that out. Apologies!


        🦛

Re^3: Dereferencing in blessed object
by haukex (Archbishop) on Feb 28, 2021 at 07:36 UTC
    The main script has use strict; but it seems that does then apply to the module it calls...I know that now!

    The scope of strict and many other pragmas like warnings is lexical: if you put it at the top of a file, its scope is only that file, and does not extend to any files included with do, require, or use.

    But - I feel sure there is a more elegant way!

    Though I concur with hippo that braces are easiest here, in the spirit of TIMTOWTDI note there's also the /e modifier: $db =~ s/_.+?_/"_$dbname"."_"/e;