Chaoui05 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I have a beginner question : How to add an object or a variable in an Array with Perl. I'am using Selenium::Screenshot package I have my Object here ..
my $white = Selenium::Screenshot->new( png => $driver->screenshot, exclude => [{ size => { width => 100, height => 100 }, # s +ize => $elem->get_size, location => { x => 5, y => 5 }, #location => $el +em->get_element_location, }] ); $white->save( file => "snapScreenshot-$browser" );
That i would like to retrieve in this Array. But i'am not sure it's well done ..?
$screenshots{$browser} = new Selenium::Screenshot;
Thanks
Lost in translation

Replies are listed 'Best First'.
Re: Pass an object, a variable in an Array
by stevieb (Canon) on May 26, 2016 at 14:50 UTC

    $screenshots as depicted in $screenshots{$browser} is not an array, it's a hash. So you want to create a hash that contains all of the objects; is that correct?

    Here's a very basic example of how you could do this, using a dummy package for testing (I know nothing of Selenium::Screenshot).

    use warnings; use strict; use Data::Dumper; package Blah; sub new { my ($class, $browser) = @_; return bless {browser => $browser}, $class; } package main; # the example you're interested in is below my %screenshots; for my $browser ('ie', 'ff', 'chrome'){ $screenshots{$browser} = Blah->new($browser); } print Dumper \%screenshots;

    Output:

    $VAR1 = { 'ie' => bless( { 'browser' => 'ie' }, 'Blah' ), 'ff' => bless( { 'browser' => 'ff' }, 'Blah' ), 'chrome' => bless( { 'browser' => 'chrome' }, 'Blah' ) };
      Thanks a lot ! Your approach is pretty good !
      Lost in translation