Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:04:49+00:00 2026-06-07T11:04:49+00:00

When I run this Perl script: #!/bin/perl use XML::Bare; $ob = new XML::Bare(text=>'<xml><name>Bob</name></xml>’); for

  • 0

When I run this Perl script:

#!/bin/perl 
use XML::Bare; 
$ob = new XML::Bare(text=>'<xml><name>Bob</name></xml>'); 
for $i (keys %{$ob->{xml}}) {print "KEY: $i\n";} 

I get no output. However, if I put $ob inside a my():

#!/bin/perl 
use XML::Bare; 
my($ob) = new XML::Bare(text=>'<xml><name>Bob</name></xml>'); 
for $i (keys %{$ob->{xml}}) {print "KEY: $i\n";} 

I get this output:

KEY: _z 
KEY: _i 
KEY: xml 
KEY: _pos 

Why does my() change this behavior so drastically, especially given
that I’m at the top level where my() should have no effect at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:04:51+00:00Added an answer on June 7, 2026 at 11:04 am

    First of all, you should always begin your Perl scripts with

    use strict;
    use warnings;
    

    This will force you to declare all your variables with my which catches many typos and simple mistakes.

    In your case, it’s not actually the my that causes the changed behavior, but the parentheses, which put $ob in list context.

    Looking at the source of XML::Bare, we find this in the constructor:

    sub new { 
        ...
        bless $self, $class;
        return $self if ( !wantarray );
        return ( $self, $self->parse() );
    }
    

    Notice that the second return line calls ->parse on the new object for you, which you forgot to do in your first example, which is why that one didn’t have any data in it.

    So you can say

    my $obj = XML::Bare->new(text=>'<xml><name>Bob</name></xml>'); # in scalar context
    $obj->parse;
    

    Or

    my ( $obj ) = XML::Bare->new(text=>'<xml><name>Bob</name></xml>'); # in list context
    

    and they should be equivalent.

    This is a pretty strange interface choice, but I’m not familiar with XML::Bare.

    Notice also that I’ve avoided indirect method syntax (XML::Bare->new instead of new XML::Bare). This helps to avoid some nasty problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run this Perl code: #!/usr/bin/perl print content-type: text/html \n\n; print Hello World.\n;
Here is an example: this script would generate data. #!/opt/local/bin/perl use Data::Dumper; use strict;
How can I run my perl CGI script without apache? This is for testing
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
Has anyone run perl script given at http://oreilly.com/pub/h/974#code ? This is a famous one,
I need to run bin/psql on the command line (or script) and print its
I have this script: #!/usr/bin/perl $LOGFILE = Soccer.txt; open(LOGFILE) or die(Could not open log
I cannot get this to work. #!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
I'm trying to run this Perl script but it is not running as required.
When I set my locale to it_IT.UTF-8 (export LC_ALL=it_IT.UTF-8) and run this script #!/usr/bin/env

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.