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 6553747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:37:07+00:00 2026-05-25T12:37:07+00:00

Puns aside, I’m trying to implement an import method in my Perl class to

  • 0

Puns aside, I’m trying to implement an import method in my Perl class to instantiate a Data object, which is essentially a glorified array of hashrefs, from a proper array of hashrefs.

Here’s an example of how I plan to use it:

# Pull in the data
my $data = Data->import(
                         [
                           { a => 1, b => 7, c => 3},
                           { a => 7, b => 9, c => 2},
                         ]
                       );

$data->manipulate;  # Use package methods

My import implementation is as follows:

package Data;

sub initialize {
    my $class = shift;
    my $data = [];
    bless $data, $class;
    return $data;
}

sub import {

    my ( $class, $data ) = @_;

    bless $data, $class;
    return $data;
}

1;

The surprising thing is that Perl reports the error at compile-time (note the BEGIN block):

Can't bless non-reference value at Data.pm line 51.
BEGIN failed--compilation aborted at myScript.pl line 8.

perldiag didn’t add much clarity to what’s going on:

Can’t bless non-reference value

(F) Only hard references may be blessed. This is how Perl “enforces”
encapsulation of objects. See
perlobj.


I even tried initializing the object and adding the data in two separate steps:

sub import {                         #< Another constructor >

    my ( $class, $data ) = @_;

    my $obj = $class->initialize;
    push @$obj, @$data;

    return $obj;
}

This resulted in the following compile-time error:

Can't use an undefined value as an ARRAY reference...
BEGIN failed--compilation aborted at...

Two questions:

  1. What’s wrong with what I’ve done?
  2. Could someone please clarify the perldiag explanation of this compile-time error?
  • 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-05-25T12:37:08+00:00Added an answer on May 25, 2026 at 12:37 pm

    import() is a dangerous name to give a method, as Perl sometimes calls a method called ‘import’ for you. In fact, the statement:

    use Data;
    

    really means:

    BEGIN {
        require Data;
        Data->import();
    }
    

    which happens during ‘compilation’.

    So it’s likely that your import() method is being called by Perl as part of the class loading mechanism before you get a chance to use it. You should rename your import() method to something else (e.g. import_data) and you should be fine.

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

Sidebar

Related Questions

Newbie question, you've been warned! I'm trying to implement a sample Rails app with
I got Go to compile: 0 known bugs; 0 unexpected bugs and typed in
I've got some code that uses type-punning to avoid having to call a member
The user sign up of my app displays success or error messages stored in
Suppose I were designing a web service with modest security requirements. For the most

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.