no strict 'refs' need help

Discussion in 'Perl Misc' started by bpatton, Jul 12, 2007.

  1. bpatton

    bpatton Guest

    I'm trying to get this function to work. It's a snippett of my
    original code, but functional. I'm at home and don't have my perl
    book with me to look it up.
    Here's the code.
    package foo;
    use strict;
    use warnings;
    use Data::Dumper;
    use vars qw ( $BY $INTO $VALUE );


    my %input = ( by => 'by' ,
    into => 'into' ,
    value => 'value',
    );

    setKnownVariables(\%input);
    print "by = $::BY\n";
    print "into = $::INTO\n";
    print "value = $::VALUE\n";

    sub setKnownVariables($) {
    my ($input) = @_;
    my ($var,$uvar);
    foreach $var (
    'allow45',
    'by',
    'coincidenceOK',
    'comment',
    'filter',
    'into' ,
    'layer',
    'lineEnd',
    'lpad',
    'lpadRedundant',
    'metalSys' ,
    'overlap',
    'overlapOK',
    'process' ,
    'ruleGroupNumber' ,
    'ruleName',
    'sideLength',
    'sideSpace',
    'square',
    'technology' ,
    'touching',
    'value',
    'width',
    ) {
    no strict 'refs';
    $uvar = $var;
    $uvar =~ tr/[a-z]/[A-Z]/;
    if (exists $input->{$var}) {
    $::${$uvar} = $input->{$var};
    delete $input->{$var};
    } else {
    $::{$var} = undef;
    }
    }
    }
    bpatton, Jul 12, 2007
    #1
    1. Advertising

  2. Re: no strict 'refs' need help

    On Jul 12, 12:46 pm, bpatton <> wrote:

    > I'm trying to get this function to work.


    In what way does it fail?

    > I'm at home and don't have my perl
    > book with me to look it up.


    Do you have a computer with Perl on it? If so you could use the
    standard reference manuals that are installed as part of Perl.

    > {
    > no strict 'refs';
    > $uvar = $var;
    > $uvar =~ tr/[a-z]/[A-Z]/;
    > if (exists $input->{$var}) {
    > $::${$uvar} = $input->{$var};
    > delete $input->{$var};
    > } else {
    > $::{$var} = undef;
    > }
    > }


    A non-existent element of a hash is undef anyhow.

    Did you really mean to create the upper case variable if the element
    in %input was there and the lower case one if not? I'm guessing not.

    There's a built-in uppercase function.

    $::${$uvar} is not valid syntax.

    {
    no strict 'refs';
    ${"::\U$var"} = delete $input->{$var};
    }

    BTW: You need to have a damn good reason to be playing with symrefs in
    the main:: namespace. I'd guess it's ~90% probable that you don't.
    Brian McCauley, Jul 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    7
    Views:
    311
    Andre Kostur
    Jun 8, 2005
  2. John Nagle
    Replies:
    3
    Views:
    281
    John Nagle
    Feb 25, 2007
  3. Daniel Friedman

    Q: "my" variables and "no strict 'refs'"

    Daniel Friedman, Jun 26, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    134
    Eric Schwartz
    Jun 26, 2003
  4. Noel Sant

    Array of hashes and strict refs - help!

    Noel Sant, Jan 30, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    255
    Ben Morrow
    Jan 30, 2004
  5. Billy Patton

    using no strict "refs"

    Billy Patton, Jan 10, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    195
    Mumia W. (on aioe)
    Jan 11, 2007
Loading...

Share This Page