in reply to how to assign hash element value to scalar?

I can see a couple more issues.

One is that, when (as I mentioned) you use strict; and use warnings;, you get:

Global symbol "$userid" requires explicit package name at x line 17. Global symbol "$password" requires explicit package name at x line 18. Global symbol "$projectid" requires explicit package name at x line 19 +. Global symbol "$ordertypeid" requires explicit package name at x line +20. Global symbol "$orderdateid" requires explicit package name at x line +21. Global symbol "$accountid" requires explicit package name at x line 22 +. Global symbol "$segcodeid" requires explicit package name at x line 23 +. Global symbol "$marketid" requires explicit package name at x line 24. Global symbol "$marketday" requires explicit package name at x line 25 +. Global symbol "$marketmonth" requires explicit package name at x line +26. Global symbol "$marketyear" requires explicit package name at x line 2 +7. Global symbol "$putcall" requires explicit package name at x line 28. Global symbol "$strikeprice" requires explicit package name at x line +29. Global symbol "$bs" requires explicit package name at x line 30. Global symbol "$quantity" requires explicit package name at x line 31. Global symbol "$orderprice" requires explicit package name at x line 3 +2. Global symbol "$entered_by" requires explicit package name at x line 3 +4. Global symbol "$resultid" requires explicit package name at x line 80. Global symbol "$resultdesc" requires explicit package name at x line 8 +1. Global symbol "$orderid" requires explicit package name at x line 82.

Of these, note that $resultid is defined within the if clause:

if ($response->{channel}->{error}->{errorid} == "") { # ... my $resultid = $response->{channel}->{result}->{resultid};\ # ... };

... so of *course* it's not going to be available outside of the block.

You're also trying to return from outside of a subroutine, as well as comparing a string using "==" in the if statement I referenced above (you should really either use eq for string comparisons, or change "" to the numeric 0).

Is this the whole code sample, or just part of it?

And what happens when [id://jugdish114|you] add use strict; and use warnings; to it?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: how to assign hash element value to scalar?
by jugdish114 (Initiate) on Nov 14, 2006 at 20:14 UTC
    thanks, yes this is part of a subroutine, here's the beginning of it:
    sub enter_order { my ($userid) = $_[0]; my ($password) = $_[1]; my ($projectid) = $_[2]; my ($ordertypeid) = $_[3]; my ($orderdateid) = $_[4]; my ($accountid) = $_[5]; my ($segcodeid) = $_[6]; my ($marketid) = $_[7]; my ($marketday) = $_[8]; my ($marketmonth) = $_[9]; my ($marketyear) = $_[10]; my ($putcall) = $_[11]; my ($strikeprice) = $_[12]; my ($bs) = $_[13]; my ($quantity) = $_[14]; my ($orderprice) = $_[15]; my ($entered_by) = $_[16];

    yes, when i add 'use strict' it is not very pretty, some of the code outside the subroutine give errors.

    and thanks for pointing out the error with the "==" rather than "eq" i think i write in too many languages to keep them straight.

    see ya,
          when i add 'use strict' it is not very pretty, some of the code outside the subroutine give errors.

      You don't really believe that by not adding 'use strict' it means the errors magically go away, do you? :-)


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Ok, Ok, Liverpole wins, it was his gently embarassing remarks that got me to clean up the error when 'use strict' was added back in the code. Once I worked through all the global and local referencing, the code behaved pretty well. So my syntax was correct, it seems that Perl was getting confused somewhere because of lack of variable definition.

        Looks good, thanks again
        fc
      Well, as an aside ... if you pass that many parameters to a subroutine, have you considered using a hash and using name => value pairs? That would greatly reduce the risk of errors and makes all these variable declarations unnecessary ...

      I was thinking of something like the following ... suitably adjusted to your concrete requirements of course (are all parameters necessary, should they get a default value if not given, ...)

      # call the sub my_sub(userid => 'foo', password => 'bar'); sub my_sub { my %default_values = (userid => 'user', password => 'passwd'); my %param = (%default_values, @_); print $param{userid}, $/; print $param{password}, $/; # ... } # or with all required arguments sub my_sub { my @required = qw/userid password/; my %param = @_; for my $req (@required) { die "Subroutine my_sub called without required parameter: $req" unless exists $param{$req}; } print $param{userid}, $/; print $param{password}, $/; # ... }

      -- Hofmator

      Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      It may be that you want to explicitly show which element of the subroutine's arguments is assigned to which variable but if not, you could save some typing by doing

      my ($userid, $password, $projectid, ... , $entered_by) = @_;

      I hope this is of use.

      Cheers,

      JohnGG