CINXE.COM
Import::Into - Import packages into other packages - metacpan.org
<!DOCTYPE html> <html lang="en-US"> <head> <title>Import::Into - Import packages into other packages - metacpan.org</title> <link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" /> <link href="/assets/style-XFEQ536G.css" rel="stylesheet" type="text/css"> <script src="/assets/main-WXBSEDQJ.js" type="module"></script> <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Import-Into - MetaCPAN" href="/dist/Import-Into/releases.rss" /> <link rel="canonical" href="https://metacpan.org/pod/Import::Into" /> <meta name="description" content="Import packages into other packages" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5"> <link rel="shortcut icon" href="/static/icons/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-touch-icon.png"> <link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN"> <script async src="https://perl-ads.perlhacks.com/perl-ads.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-E82Q2V8LVD"></script> <meta property="og:site_name" content="MetaCPAN" /> <meta name="twitter:site" content="@metacpan" /> <meta property="og:type" content="article" /> <meta name="twitter:card" content="summary" /> <meta property="og:url" content="https://metacpan.org/pod/Import::Into" /> <meta property="og:title" content="Import::Into" /> <meta property="og:description" content="Import packages into other packages" /> <meta property="og:image" content="https://metacpan.org/static/images/dots.png" /> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="header-logo-large hidden-xs"> <a href="/" tabindex="0"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#logo" /> </svg> </a> </div> <div class="header-logo-icon visible-xs"> <a href="/"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#dots" /> </svg> </a> </div> <ul class="nav navbar-nav menu-items hidden-xs hidden-sm"> <li><a href="/about">About</a></li> <li><a href="/about/sponsors">Sponsor</a></li> <li><a href="https://grep.metacpan.org/">grep::cpan</a></li> <li><a href="/recent">Recent</a></li> <li><a href="/about/faq">FAQ</a></li> <li><a href="/tools">Tools</a></li> <li><a href="https://fastapi.metacpan.org/">API</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <button type="button" class="searchbar-btn visible-xs visible-sm"> <i class="fa fa-search button-fa-icon"></i> </button> <form action="/search" class="searchbar-form visible-md visible-lg search-form form-horizontal"> <input type="hidden" name="size" id="metacpan_search-size" value="20"> <div class="form-group"> <div class="search-group"> <i class="fa fa-search"></i> <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value=""> </div> </div> </form> <li class="icon-slidepanel visible-xs visible-sm"> <button data-toggle="slidepanel" data-target=".slidepanel"> <span class="button-fa-icon"> <i class="fa fa-bars slidepanel-open"></i> <i class="fa fa-times slidepanel-close"></i> </span> </button> </li> <li class="dropdown login-dropdown show-logged-in"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li><a href="/account/identities">Identities</a></li> <li><a href="/account/profile">Profile</a></li> <li><a href="/account/favorite/list">Favorites</a></li> <li> <a href="#" type="button" class="logout-button"> Logout </a> </li> </ul> </li> <li class="dropdown login-dropdown show-logged-out"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon avatar-placeholder" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li> <a href="/login/github"> <i class="fab fa-github fa-fw"></i> GitHub </a> </li> <li> <a href="/login/twitter"> <i class="fab fa-twitter fa-fw"></i> Twitter </a> </li> <li> <a href="/login/google"> <i class="fab fa-google fa-fw"></i> Google </a> </li> </ul> </li> <li class="dropdown login-dropdown hide-logged-in hide-logged-out"> <button> <i class="fa fa-user button-fa-icon" aria-hidden="true"></i> </button> </li> </ul> </nav> <div class="page-content "> <div id="perl-ad-target" class="top-notify-banner perl-ad-target"> </div> <nav class="sidebar"> <div class="slidepanel"> <ul class="nav-list "> <li class="nav-header no-margin-top"> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Import-Into was released."> <span class="relatize">28 Aug 2015 00:30:22 UTC</span> </div> </li> <li> Distribution: <a href="/dist/Import-Into">Import-Into</a> </li> <li> Module version: 1.002005 </li> <li> <a data-keyboard-shortcut="g s" href="/dist/Import-Into/source/lib/Import/Into.pm">Source</a> (<a href="/dist/Import-Into/source/lib/Import/Into.pm?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g b" href="/dist/Import-Into/source/lib/Import">Browse</a> (<a href="/dist/Import-Into/source/lib/Import?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g c" href="/dist/Import-Into/changes">Changes</a> </li> <li> <a class="nopopup" href="/dist/Import-Into/contribute">How to Contribute</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/Import-Into.git">Repository</a> (<a rel="noopener nofollow" href="git://git.shadowcat.co.uk/p5sagit/Import-Into.git">git clone</a>) </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Import-Into">Issues</a> (1) </li> <li> <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Import-Into+1.002005" title="Matrix">Testers</a> <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/I/Import-Into.html?oncpan=1&distmat=1&version=1.002005&grade=2" style="color: #090">2137</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/I/Import-Into.html?oncpan=1&distmat=1&version=1.002005&grade=3" style="color: #900">2</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/I/Import-Into.html?oncpan=1&distmat=1&version=1.002005&grade=4">0</a>)</span> </li> <li> <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/HAARG/Import-Into-1.002005">Kwalitee</a> </li> <li> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Import-Into who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)"> Bus factor: 4 </div> </li> <li> <a rel="noopener nofollow" href="http://cpancover.com/latest/Import-Into-1.002005/index.html">79.66% Coverage </a> </li> <li> License: perl_5 </li> <li> Perl: v5.6.0 </li> <li class="nav-header">Activity</li> <li> <div class="activity-graph"> <img src="/dist/Import-Into/activity.svg?res=month" /> <div class="comment">24 month</div> </div> </li> <li class="nav-header">Tools</li> <li> <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/H/HA/HAARG/Import-Into-1.002005.tar.gz"> Download (<span itemprop="fileSize">7.57KB</span>)</a> </li> <li> <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FHAARG%2FImport-Into-1.002005%2Flib%2FImport%2FInto.pm"> MetaCPAN Explorer </a> </li> <li> <a href="/dist/Import-Into/permissions"> Permissions </a> </li> <li> <a href="/dist/Import-Into/releases.rss"> Subscribe to distribution </a> </li> <li> <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog"> Install Instructions </button> </li> <li> <form action="/search"> <input type="hidden" name="q" value="dist:Import-Into"> <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li> <form action="https://grep.metacpan.org/search"> <input type="hidden" name="qd" value="Import-Into"> <input type="hidden" name="source" value="metacpan"> <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li class="version-jump"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Jump to version</option> <option disabled value="/release/HAARG/Import-Into-1.002005/view/lib/Import/Into.pm" >1.002005 (HAARG on 2015-08-28)</option> <option value="/release/ETHER/Import-Into-1.002004/view/lib/Import/Into.pm" >1.002004 (ETHER on 2014-07-09)</option> <option value="/release/HAARG/Import-Into-1.002001/view/lib/Import/Into.pm" >1.002001 (HAARG on 2014-03-04)</option> <option value="/release/HAARG/Import-Into-1.002000/view/lib/Import/Into.pm" >1.002000 (HAARG on 2013-12-10)</option> <option value="/release/MSTROUT/Import-Into-1.001000/view/lib/Import/Into.pm" >1.001000 (MSTROUT on 2012-05-12)</option> <option value="/release/MSTROUT/Import-Into-1.000003/view/lib/Import/Into.pm" >1.000003 (MSTROUT on 2012-05-06)</option> <option value="/release/MSTROUT/Import-Into-1.000002/view/lib/Import/Into.pm" >1.000002 (MSTROUT on 2012-05-05)</option> <optgroup label="BackPAN">' <option value="/release/ETHER/Import-Into-1.002003/view/lib/Import/Into.pm" >1.002003 (ETHER on 2014-07-09)</option> <option value="/release/ETHER/Import-Into-1.002002/view/lib/Import/Into.pm" >1.002002 (ETHER on 2014-05-06)</option> <option value="/release/ETHER/Import-Into-1.001001/view/lib/Import/Into.pm" >1.001001 (ETHER on 2013-03-26)</option> <option value="/release/MSTROUT/Import-Into-1.000001/view/lib/Import/Into.pm" >1.000001 (MSTROUT on 2012-05-03)</option> <option value="/release/MSTROUT/Import-Into-1.000000/view/lib/Import/Into.pm" >1.000000 (MSTROUT on 2012-05-03)</option> </optgroup> </select> </li> <li class="version-diff"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Diff with version</option> <option disabled value="/release/HAARG/Import-Into-1.002005/diff/HAARG/Import-Into-1.002005/lib/Import/Into.pm" >1.002005 (HAARG on 2015-08-28)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/ETHER/Import-Into-1.002004/lib/Import/Into.pm" >1.002004 (ETHER on 2014-07-09)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/HAARG/Import-Into-1.002001/lib/Import/Into.pm" >1.002001 (HAARG on 2014-03-04)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/HAARG/Import-Into-1.002000/lib/Import/Into.pm" >1.002000 (HAARG on 2013-12-10)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/MSTROUT/Import-Into-1.001000/lib/Import/Into.pm" >1.001000 (MSTROUT on 2012-05-12)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/MSTROUT/Import-Into-1.000003/lib/Import/Into.pm" >1.000003 (MSTROUT on 2012-05-06)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/MSTROUT/Import-Into-1.000002/lib/Import/Into.pm" >1.000002 (MSTROUT on 2012-05-05)</option> <optgroup label="BackPAN">' <option value="/release/HAARG/Import-Into-1.002005/diff/ETHER/Import-Into-1.002003/lib/Import/Into.pm" >1.002003 (ETHER on 2014-07-09)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/ETHER/Import-Into-1.002002/lib/Import/Into.pm" >1.002002 (ETHER on 2014-05-06)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/ETHER/Import-Into-1.001001/lib/Import/Into.pm" >1.001001 (ETHER on 2013-03-26)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/MSTROUT/Import-Into-1.000001/lib/Import/Into.pm" >1.000001 (MSTROUT on 2012-05-03)</option> <option value="/release/HAARG/Import-Into-1.002005/diff/MSTROUT/Import-Into-1.000000/lib/Import/Into.pm" >1.000000 (MSTROUT on 2012-05-03)</option> </optgroup> </select> </li> <li> <ul class="dependencies"> <li class="nav-header">Dependencies</li> <li><a href="/pod/Module::Runtime" title="Module::Runtime" class="ellipsis">Module::Runtime</a></li> <li><a href="/pod/strict" title="strict" class="ellipsis">strict</a></li> <li><a href="/pod/warnings" title="warnings" class="ellipsis">warnings</a></li> <li> <hr> </li> <li> <a href="/module/Import::Into/requires">Reverse dependencies</a> </li> <li> <a href="http://deps.cpantesters.org/?module=Import%3A%3AInto">CPAN Testers List</a> </li> <li> <a href="https://cpandeps.grinnz.com/?dist=Import-Into">Dependency graph</a> </li> </ul> </li> <li class="nav-header">Permalinks</li> <li> <a href="/release/HAARG/Import-Into-1.002005/view/lib/Import/Into.pm">This version</a> </li> <li> <a href="/pod/Import::Into">Latest version</a> </li> <li> <div class="plussers"> <div class="nav-header">++ed by:</div> <div> <a class="display-all" href="/author/MARCC"><img src="https://www.gravatar.com/avatar/2c41ef5501c331e443eba673c765ac22?d=identicon&s=20" title="MARCC" alt="MARCC"></a> <a class="display-all" href="/author/JAYALLEN"><img src="https://www.gravatar.com/avatar/34e155145f5145d2e30979313b334327?d=identicon&s=20" title="JAYALLEN" alt="JAYALLEN"></a> <a class="display-all" href="/author/DOLMEN"><img src="https://www.gravatar.com/avatar/70d9b050bfe39350c234d710fadfcd39?d=identicon&s=20" title="DOLMEN" alt="DOLMEN"></a> <a class="display-all" href="/author/ADAMJS"><img src="https://www.gravatar.com/avatar/62c7b6e864d6509cff5839543c676614?d=identicon&s=20" title="ADAMJS" alt="ADAMJS"></a> <a class="display-all" href="/author/MELO"><img src="https://www.gravatar.com/avatar/4809188f41ad2613f4c240b72d97604c?d=identicon&s=20" title="MELO" alt="MELO"></a> </div> <!-- Display counts of plussers--> <div> <a href="/dist/Import-Into/plussers">21 PAUSE users</a> </div> <div> 17 non-PAUSE users </div> </div> </li> <li> <div> <button class="contributors-show-button btn-link">and 1 contributors</button> <div id="metacpan_contributors" class="slide-out slide-out-hidden"> <div> <ul> <li class="contributor" data-contrib-email="mst@shadowcat.co.uk" > <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&s=20" /> mst - Matt S. Trout (cpan:MSTROUT) </li> </ul> </div> </div> </div> </li> </ul> </div> </nav> <div class="content-navigation"> <div class="breadcrumbs"> <span> <a data-keyboard-shortcut="g a" rel="author" href="/author/HAARG" class="author-name">Graham Knop</a> </span> <span> / </span> <div class="release dist-release status-latest maturity-released"> <span class="dropdown"><b class="caret"></b></span> <select class="select-navigator "> <option selected value="/release/HAARG/Import-Into-1.002005/view/lib/Import/Into.pm" >1.002005 (HAARG on 2015-08-28)</option> <option value="/release/ETHER/Import-Into-1.002004/view/lib/Import/Into.pm" >1.002004 (ETHER on 2014-07-09)</option> <option value="/release/HAARG/Import-Into-1.002001/view/lib/Import/Into.pm" >1.002001 (HAARG on 2014-03-04)</option> <option value="/release/HAARG/Import-Into-1.002000/view/lib/Import/Into.pm" >1.002000 (HAARG on 2013-12-10)</option> <option value="/release/MSTROUT/Import-Into-1.001000/view/lib/Import/Into.pm" >1.001000 (MSTROUT on 2012-05-12)</option> <option value="/release/MSTROUT/Import-Into-1.000003/view/lib/Import/Into.pm" >1.000003 (MSTROUT on 2012-05-06)</option> <option value="/release/MSTROUT/Import-Into-1.000002/view/lib/Import/Into.pm" >1.000002 (MSTROUT on 2012-05-05)</option> <optgroup label="BackPAN">' <option value="/release/ETHER/Import-Into-1.002003/view/lib/Import/Into.pm" >1.002003 (ETHER on 2014-07-09)</option> <option value="/release/ETHER/Import-Into-1.002002/view/lib/Import/Into.pm" >1.002002 (ETHER on 2014-05-06)</option> <option value="/release/ETHER/Import-Into-1.001001/view/lib/Import/Into.pm" >1.001001 (ETHER on 2013-03-26)</option> <option value="/release/MSTROUT/Import-Into-1.000001/view/lib/Import/Into.pm" >1.000001 (MSTROUT on 2012-05-03)</option> <option value="/release/MSTROUT/Import-Into-1.000000/view/lib/Import/Into.pm" >1.000000 (MSTROUT on 2012-05-03)</option> </optgroup> </select> <a data-keyboard-shortcut="g d" class="release-name" href="/dist/Import-Into">Import-Into-1.002005</a> </div> <span class="river-gauge-gauge"> <svg width="24px" height="15px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <title> River stage three • 143 direct dependents • 856 total dependents </title> <rect x="0" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="5" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="10" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="15" y="0" width="4" height="15" fill="#e4e2e2" /> <rect x="20" y="0" width="4" height="15" fill="#e4e2e2" /> </g> </svg> </span> <div id="Import-Into-fav" class="show-logged-in"> <form action="/account/favorite/add" style="display: inline" method="POST"> <input type="hidden" name="remove" value="0"> <input type="hidden" name="release" value="Import-Into-1.002005"> <input type="hidden" name="author" value="HAARG"> <input type="hidden" name="distribution" value="Import-Into"> <button type="submit" class="favorite highlight"><span>38</span> ++</button> </form> </div> <div class="show-logged-out"> <button class="fav-not-logged-in favorite highlight"><span>38</span> ++</button> </div> / <span>Import::Into</span> </div> </div> <main class="content"> <nav class="toc"> <div class="toc-header"><strong>Contents</strong></div> <ul> <li><a href="#NAME">NAME</a></li> <li><a href="#SYNOPSIS">SYNOPSIS</a></li> <li><a href="#DESCRIPTION">DESCRIPTION</a></li> <li><a href="#METHODS">METHODS</a> <ul> <li><a href="#%24package-%3Eimport%3A%3Ainto(-%24target%2C-%40arguments-)%3B">$package->import::into( $target, @arguments );</a></li> <li><a href="#%24package-%3Eunimport%3A%3Aout_of(-%24target%2C-%40arguments-)%3B">$package->unimport::out_of( $target, @arguments );</a></li> </ul> </li> <li><a href="#WHY-USE-THIS-MODULE">WHY USE THIS MODULE</a></li> <li><a href="#SEE-ALSO">SEE ALSO</a></li> <li><a href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a></li> <li><a href="#AUTHOR">AUTHOR</a></li> <li><a href="#CONTRIBUTORS">CONTRIBUTORS</a></li> <li><a href="#COPYRIGHT">COPYRIGHT</a></li> <li><a href="#LICENSE">LICENSE</a></li> </ul></nav> <div class="pod anchors"> <h1 id="NAME">NAME</h1> <p>Import::Into - Import packages into other packages</p> <h1 id="SYNOPSIS">SYNOPSIS</h1> <pre><code>package My::MultiExporter; use Import::Into; # simple sub import { Thing1->import::into(scalar caller); } # multiple sub import { my $target = caller; Thing1->import::into($target); Thing2->import::into($target, qw(import arguments)); } # by level sub import { Thing1->import::into(1); } # with exporter use base qw(Exporter); sub import { shift->export_to_level(1); Thing1->import::into(1); } # no My::MultiExporter == no Thing1 sub unimport { Thing1->unimport::out_of(scalar caller); }</code></pre> <p>People wanting to re-export your module should also be using <a href="/pod/Import::Into">Import::Into</a>. Any exporter or pragma will work seamlessly.</p> <p>Note: You do <b>not</b> need to make any changes to Thing1 to be able to call <code>import::into</code> on it. This is a global method, and is callable on any package (and in fact on any object as well, although it's rarer that you'd want to do that).</p> <h1 id="DESCRIPTION">DESCRIPTION</h1> <p>Writing exporters is a pain. Some use <a href="/pod/Exporter">Exporter</a>, some use <a href="/pod/Sub::Exporter">Sub::Exporter</a>, some use <a href="/pod/Moose::Exporter">Moose::Exporter</a>, some use <a href="/pod/Exporter::Declare">Exporter::Declare</a> ... and some things are pragmas.</p> <p>Exporting on someone else's behalf is harder. The exporters don't provide a consistent API for this, and pragmas need to have their import method called directly, since they effect the current unit of compilation.</p> <p><code>Import::Into</code> provides global methods to make this painless.</p> <h1 id="METHODS">METHODS</h1> <h2 id="$package->import::into(-$target,-@arguments-);"><a id="import"></a><a id="package-import::into-target-arguments"></a>$package->import::into( $target, @arguments );</h2> <p>A global method, callable on any package. Loads and imports the given package into <code>$target</code>. <code>@arguments</code> are passed along to the package's import method.</p> <p><code>$target</code> can be an package name to export to, an integer for the caller level to export to, or a hashref with the following options:</p> <dl> <dt id="package">package</dt> <dd> <p>The target package to export to.</p> </dd> <dt id="filename">filename</dt> <dd> <p>The apparent filename to export to. Some exporting modules, such as <a href="/pod/autodie">autodie</a> or <a href="/pod/strictures">strictures</a>, care about the filename they are being imported to.</p> </dd> <dt id="line">line</dt> <dd> <p>The apparent line number to export to. To be combined with the <code>filename</code> option.</p> </dd> <dt id="level">level</dt> <dd> <p>The caller level to export to. This will automatically populate the <code>package</code>, <code>filename</code>, and <code>line</code> options, making it the easiest most constent option.</p> </dd> <dt id="version">version</dt> <dd> <p>A version number to check for the module. The equivalent of specifying the version number on a <code>use</code> line.</p> </dd> </dl> <h2 id="$package->unimport::out_of(-$target,-@arguments-);"><a id="unimport"></a><a id="package-unimport::out_of-target-arguments"></a>$package->unimport::out_of( $target, @arguments );</h2> <p>Equivalent to <code>import::into</code>, but dispatches to <code>$package</code>'s <code>unimport</code> method instead of <code>import</code>.</p> <h1 id="WHY-USE-THIS-MODULE"><a id="WHY"></a>WHY USE THIS MODULE</h1> <p>The APIs for exporting modules aren't consistent. <a href="/pod/Exporter">Exporter</a> subclasses provide export_to_level, but if they overrode their import method all bets are off. <a href="/pod/Sub::Exporter">Sub::Exporter</a> provides an into parameter but figuring out something used it isn't trivial. Pragmas need to have their <code>import</code> method called directly since they affect the current unit of compilation.</p> <p>It's ... annoying.</p> <p>However, there is an approach that actually works for all of these types.</p> <pre><code>eval "package $target; use $thing;"</code></pre> <p>will work for anything checking caller, which is everything except pragmas. But it doesn't work for pragmas - pragmas need:</p> <pre><code>$thing->import;</code></pre> <p>because they're designed to affect the code currently being compiled - so within an eval, that's the scope of the eval itself, not the module that just <code>use</code>d you - so</p> <pre><code>sub import { eval "use strict;" }</code></pre> <p>doesn't do what you wanted, but</p> <pre><code>sub import { strict->import; }</code></pre> <p>will apply <a href="/pod/strict">strict</a> to the calling file correctly.</p> <p>Of course, now you have two new problems - first, that you still need to know if something's a pragma, and second that you can't use either of these approaches alone on something like <a href="/pod/Moose">Moose</a> or <a href="/pod/Moo">Moo</a> that's both an exporter and a pragma.</p> <p>So, a solution for that is:</p> <pre><code>use Module::Runtime; my $sub = eval "package $target; sub { use_module(shift)->import(\@_) }"; $sub->($thing, @import_args);</code></pre> <p>which means that import is called from the right place for pragmas to take effect, and from the right package for caller checking to work - and so behaves correctly for all types of exporter, for pragmas, and for hybrids.</p> <p>Additionally, some import routines check the filename they are being imported to. This can be dealt with by generating a <a href="/pod/perlsyn#Plain-Old-Comments-(Not!)">#line directive</a> in the eval, which will change what <code>caller</code> reports for the filename when called in the importer. The filename and line number to use in the directive then need to be fetched using <code>caller</code>:</p> <pre><code>my ($target, $file, $line) = caller(1); my $sub = eval qq{ package $target; #line $line "$file" sub { use_module(shift)->import(\@_) } }; $sub->($thing, @import_args);</code></pre> <p>And you need to switch between these implementations depending on if you are targeting a specific package, or something in your call stack.</p> <p>Remembering all this, however, is excessively irritating. So I wrote a module so I didn't have to anymore. Loading <a href="/pod/Import::Into">Import::Into</a> creates a global method <code>import::into</code> which you can call on any package to import it into another package. So now you can simply write:</p> <pre><code>use Import::Into; $thing->import::into($target, @import_args);</code></pre> <p>This works because of how perl resolves method calls - a call to a simple method name is resolved against the package of the class or object, so</p> <pre><code>$thing->method_name(@args);</code></pre> <p>is roughly equivalent to:</p> <pre><code>my $code_ref = $thing->can('method_name'); $code_ref->($thing, @args);</code></pre> <p>while if a <code>::</code> is found, the lookup is made relative to the package name (i.e. everything before the last <code>::</code>) so</p> <pre><code>$thing->Package::Name::method_name(@args);</code></pre> <p>is roughly equivalent to:</p> <pre><code>my $code_ref = Package::Name->can('method_name'); $code_ref->($thing, @args);</code></pre> <p>So since <a href="/pod/Import::Into">Import::Into</a> defines a method <code>into</code> in package <code>import</code> the syntax reliably calls that.</p> <p>For more craziness of this order, have a look at the article I wrote at <a href="http://shadow.cat/blog/matt-s-trout/madness-with-methods">http://shadow.cat/blog/matt-s-trout/madness-with-methods</a> which covers coderef abuse and the <code>${\...}</code> syntax.</p> <p>And that's it.</p> <h1 id="SEE-ALSO"><a id="SEE"></a>SEE ALSO</h1> <p>I gave a lightning talk on this module (and <a href="/pod/curry">curry</a> and <a href="/pod/Safe::Isa">Safe::Isa</a>) at <a href="https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s">YAPC::NA 2013</a>.</p> <h1 id="ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</h1> <p>Thanks to Getty for asking "how can I get <code>use strict; use warnings;</code> turned on for all consumers of my code?" and then "why is this not a module?!".</p> <h1 id="AUTHOR">AUTHOR</h1> <p>mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk></p> <h1 id="CONTRIBUTORS">CONTRIBUTORS</h1> <p>haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org></p> <p>Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@gmail.com></p> <h1 id="COPYRIGHT">COPYRIGHT</h1> <p>Copyright (c) 2012 the Import::Into <a href="#AUTHOR">"AUTHOR"</a> and <a href="#CONTRIBUTORS">"CONTRIBUTORS"</a> as listed above.</p> <h1 id="LICENSE">LICENSE</h1> <p>This library is free software and may be distributed under the same terms as perl itself.</p></div> <div id="metacpan_install-instructions-dialog" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Module Install Instructions</h4> </div> <div class="modal-body"> <p>To install Import::Into, copy and paste the appropriate command in to your terminal.</p> <p><a href="/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p> <pre><code>cpanm Import::Into</code></pre> <p><a href="/pod/CPAN">CPAN shell</a></p> <pre><code>perl -MCPAN -e shell install Import::Into</code></pre> <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> </div> </div> </div> </div> </main> <div class="content-pagination"> </div> </div> <footer class="footer"> <div class="footer-container"> <div class="footer-social"> <div class="footer-link footer-logo"> <a href="/"> <img src="/static/images/metacpan-logo.svg" alt="MetaCPAN" /> </a> </div> <a class="footer-social-link" href="https://github.com/metacpan"> <i class="fab fa-github-square"></i> </a> <a class="footer-social-link" href="https://fosstodon.org/@metacpan"> <i class="fab fa-mastodon"></i> </a> </div> <div class="footer-links"> <div class="footer-link"> <a href="/about">About</a> </div> <div class="footer-link"> <a href="/about/sponsors">Sponsor</a> </div> <div class="footer-link"> <a href="https://grep.metacpan.org">grep::cpan</a> </div> <div class="footer-link"> <a href="/recent">Recent</a> </div> <div class="footer-link"> <a href="/about/faq">FAQ</a> </div> <div class="footer-link"> <a href="/tools">Tools</a> </div> <div class="footer-link"> <a href="https://fastapi.metacpan.org/">API</a> </div> <div class="footer-link"> <a href="https://www.perl.org/">Perl.org</a> </div> </div> <div class="footer-sponsors"> <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener"> <img class="footer-sponsor-bytemark" src="/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener"> <img class="footer-sponsor-liquidweb" src="/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener"> <img class="footer-sponsor-deriv" src="/static/images/sponsors/deriv.svg" alt="Deriv logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener"> <img class="footer-sponsor-geocode" src="/static/images/sponsors/geocodelogo.svg" alt="Geocode logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener"> <img class="footer-sponsor-fastly" src="/static/images/sponsors/fastly_logo.svg" alt="Fastly logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener"> <img class="footer-sponsor-opencage" src="/static/images/sponsors/open-cage.svg" alt="OpenCage logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://www.elastic.co/" rel="noopener"> <img class="footer-sponsor-elastic" src="/static/images/sponsors/elastic.svg" alt="Elastic logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://route4me.com/" rel="noopener"> <img class="footer-sponsor-route4me" src="/static/images/sponsors/route4me.png" alt="Route4Me logo"> </a> </div> </div> </footer> <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Keyboard Shortcuts</h4> </div> <div class="modal-body row"> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Global</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>s</kbd> </td> <td>Focus search bar</td> </tr> <tr> <td class="keys"> <kbd>?</kbd> </td> <td>Bring up this help dialog</td> </tr> </tbody> </table> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>GitHub</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>p</kbd> </td> <td>Go to pull requests</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>go to github issues (only if github is preferred repository)</td> </tr> </tbody> </table> </div> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>POD</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>a</kbd> </td> <td>Go to author</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>c</kbd> </td> <td>Go to changes</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>Go to issues</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>d</kbd> </td> <td>Go to dist</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>r</kbd> </td> <td>Go to repository/SCM</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>s</kbd> </td> <td>Go to source</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>b</kbd> </td> <td>Go to file browse</td> </tr> </tbody> </table> </div> <div class="col-md-12"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Search terms</th> </tr> </thead> <tbody> <tr> <td><em>module:</em> (e.g. <a href="/search?q=module%3APlugin">module:Plugin</a>)</td> </tr> <tr> <td><em>distribution:</em> (e.g. <a href="/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td> </tr> <tr> <td><em>author:</em> (e.g. <a href="/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td> </tr> <tr> <td><em>version:</em> (e.g. <a href="/search?q=version%3A1.00">version:1.00</a>)</td> </tr> </tbody> </table> </div> </div> <div class="modal-footer"></div> </div> </div> </div> </body> </html>